<template>
	<view class="devcie-rate-layout">
		<!-- 机具信息 -->
		<view class="devcie-rate-header">
			<view class="devcie-rate-header-item">
				<text>{{dataInfo.nameTitle}}</text>
				<text>{{dataInfo.name}}</text>
				<view class="line"></view>
			</view>
			<view class="devcie-rate-header-item">
				<text>机具编号</text>
				<text>{{dataInfo.code}}</text>
				<view class="line"></view>
			</view>
		</view>
		<view class="devcie-rate-container">
			<view class="policy-config-form" v-if="rateInfo.rateInfoList.length > 0">
				<view class="policy-config-form-item" v-for="(item,index) in formParams.rateList" :key="index">
					<text>{{item.retaName}}</text>
					<xh-input placeholder="请输入" inputTextAlign="center" :isLine="false" keyboardType="digit" height="0" :isSelect="!item.isCanEdit" :showRightIcon="false" v-if="formParams.isOpenEdit" v-model="item.inputValue"></xh-input>
					<text class="range-text" v-if="formParams.isOpenEdit">{{getResultValue(item).rangeValue}}</text>
					<text class="value-text" v-else>{{getResultValue(item).value}}</text>
					<view class="line policy-config-form-item-line"></view>
				</view>
			</view>
			<view class="policy-config-form-item policy-config-form-item-md" v-if="singleRateModel">
				<text>秒到提现费</text>
				<xh-input placeholder="请输入" inputTextAlign="center" :isLine="false" keyboardType="digit" v-if="formParams.isOpenEdit" v-model="singleRateModel.inputValue"></xh-input>
				<text class="range-text" v-if="formParams.isOpenEdit">{{singleRateModel.singleT0CostMin}}~{{singleRateModel.singleT0CostMax}}元/笔</text>
				<text class="value-text" v-else>{{singleRateModel.value}}元/笔</text>
			</view>
			<xh-empty minHeight="50vh" :show="rateInfo.rateInfoList.length == 0"></xh-empty>
		</view>
		<view class="devcie-rate-operate" v-if="formParams.isCanEdit">
			<view class="devcie-rate-operate-btn-box" v-if="formParams.isOpenEdit">
				<view class="submit-btn submit-btn1" @tap="save">确认保存</view>
				<view class="cancel-btn" @tap="cancel">取消</view>
			</view>
			<view class="devcie-rate-operate-btn-edit" v-if="!formParams.isOpenEdit" @tap="editRate">
				<text>修改费率</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { computed, ref } from 'vue'
	import { getMerchantRate,setMerchantRate,updateMerchantRate } from '@/common/api/api.js'

	const props = defineProps(['data'])
	const jsonData = JSON.parse(decodeURIComponent(props.data || '{}'))
	console.log(jsonData)
	const dataInfo = computed(()=>{
		if(jsonData.merchantId){
			return {
				nameTitle:'商户名称',
				name:jsonData.merchantName,
				code:jsonData.deviceNo
			}
		}
		else{
			return {
				nameTitle:'机具名称',
				name:`${jsonData.brandName}-${jsonData.produceName}(${jsonData.deviceTypeCode})`,
				code:jsonData.ksnNo
			}
		}
	})
	const rateInfo = ref({
		rateInfoList: []
	})
	const formParams = ref({
		//是否支持修改
		isCanEdit: false,
		//是否开启编辑
		isOpenEdit: false,
		//0 未配置 1 已配置
		rateStatus: '',
		// 机具费率列表
		rateList: []
	})
	// 单笔秒到提现费
	const singleRateModel = ref(null)

	// 显示不可已经设置的费率和范围显示数据
	const getResultValue = (item) => {
		if (item.retaName == '借记卡封顶') {
			item.isCanEdit = true
			return {
				value: `${item.value}元`,
				rangeValue: `${item.debitCardTopMin}~${item.debitCardTopMax}元`
			}
		} else if (item.retaName == 'creditpay') {
			item.isCanEdit = true
			return {
				value: `${item.value}%`,
				rangeValue: `${item.creditCardRateMin}~${item.creditCardRateMax}%`
			}
		} else {
			if (item.chnlType == 'HKD') {
				if (item.payTypeCode == 'nativepay') {
					///扫码支付不可修改费率
					item.isCanEdit = false
					return {
						value: `${item.value}%`,
						rangeValue: ''
					}
				} else {
					item.isCanEdit = true
					return {
						value: `${item.value}%`,
						rangeValue: `${item.debitCardRateMin}%~${item.debitCardRateMax}%`
					}
				}
			} else {
				///开店宝不可修扫码支付/云闪付费率
				if (item.payTypeCode == 'nativepay' || item.payTypeCode == 'unionpay') {
					///扫码支付/云闪付不可修改费率
					item.isCanEdit = false
					return {
						value: `${item.value}%`,
						rangeValue: ''
					}
				} else {
					item.isCanEdit = true
					return {
						value: `${item.value}%`,
						rangeValue: `${item.debitCardRateMin}%~${item.debitCardRateMax}%`
					}
				}
			}
		}
	}
	// 修改费率
	const editRate = () => {
		formParams.value.isOpenEdit = true
	}
	//取消
	const cancel = () => {
		if (formParams.value.rateStatus == '0') {
			uni.navigateBack()
		} else {
			formParams.value.isOpenEdit = false
			formParams.value.rateList.forEach(item => {
				item.inputValue = item.value
			})
			singleRateModel.value.inputValue = singleRateModel.value.value
		}
	}
	// 保存
	const save = async () => {
		if (formParams.value.rateList.length == 0) {
			uni.$utils.showToast('保存失败，暂无费率信息')
			return
		}
		for (let i = 0; i < formParams.value.rateList.length; i++) {
			const obj = formParams.value.rateList[i]
			if (uni.$uv.trim(obj.inputValue) == '') {
				uni.$utils.showToast(`请输入${obj.retaName}`)
				return
			} else {
				const value1 = parseFloat(obj.inputValue)
				if (obj.payTypeCode == '0') {
					const value2 = parseFloat(obj.debitCardTopMax)
					const value3 = parseFloat(obj.debitCardTopMin)
					if (value1 > value2 || value1 < value3) {
						uni.$utils.showToast(`${obj.retaName}超出区间范围`)
						return;
					}
				} else {
					const value2 = parseFloat(obj.creditCardRateMax)
					const value3 = parseFloat(obj.creditCardRateMin)
					if(obj.chnlType == 'HKD') {
						if ((value1 > value2 || value1 < value3) && obj.payTypeCode != 'nativepay') {
							uni.$utils.showToast(`${obj.retaName}超出区间范围`)
							return;
						}
					}
					else{
						if ((value1 > value2 || value1 < value3) && (obj.payTypeCode != 'nativepay' || obj.payTypeCode != 'unionpay')) {
							uni.$utils.showToast(`${obj.retaName}超出区间范围`)
							return;
						}
					}
					
				}
			}
		}
		if (uni.$uv.trim(singleRateModel.value.inputValue) == '') {
			uni.$utils.showToast(`请输入${singleRateModel.value.retaName}`)
			return
		}
		const value1 = parseFloat(singleRateModel.value.inputValue)
		const value2 = parseFloat(singleRateModel.value.singleT0CostMax)
		const value3 = parseFloat(singleRateModel.value.singleT0CostMin)
		if (!singleRateModel && (value1 > value2 || value1 < value3)) {
			uni.$utils.showToast(`${singleRateModel.value.retaName}超出区间范围`)
			return;
		}
		let datas = []
		let topModel = null
		formParams.value.rateList.forEach(item=>{
			if(item.payTypeCode == '0') {
				topModel = item
			}
			else{
				datas.push(item)
			}
		})
		let rateInfoList = []
		datas.forEach(obj=>{
			let dict = {}
			dict.payTypeId = obj.payTypeId
			if(!uni.$uv.test.empty(formParams.value.rateStatus)){
				dict.rateStatus = formParams.value.rateStatus
			}
			if(!uni.$uv.test.empty(obj.retaId)){
				dict.id = obj.retaId
			}
			if(obj.payTypeCode == 'creditpay') {
				if(singleRateModel.value){
					dict.mercSingleT0Cost = singleRateModel.value.inputValue
				}
				else{
					dict.mercSingleT0Cost = '0'
				}
				dict.mercCreditCardRate = obj.inputValue
				dict.mercDebitCardRate = obj.inputValue
				dict.mercDebitCardTop = '0'
				dict.mercCreditCardTop = '0'
			}
			else if(obj.payTypeCode == 'debitpay') {
				if(topModel){
					dict.mercDebitCardTop = topModel.inputValue
				}
				else{
					dict.mercDebitCardTop = '0'
				}
				dict.mercDebitCardRate = obj.inputValue
				dict.mercCreditCardRate = obj.inputValue
				dict.mercCreditCardTop = '0'
				dict.mercSingleT0Cost = '0'
			}
			else{
				dict.mercDebitCardRate = obj.inputValue
				dict.mercCreditCardRate = obj.inputValue
				dict.mercDebitCardTop = '0'
				dict.mercCreditCardTop = '0'
				dict.mercSingleT0Cost = '0'
			}
			rateInfoList.push(dict)
		})
		let params = {}
		if (jsonData.merchantId) {
			params.ksnNo = jsonData.deviceNo
			params.merId = jsonData.merchantId
		} else {
			params.ksnNo = jsonData.ksnNo
		}
		params.rateInfoList = rateInfoList
		if(formParams.value.rateStatus == '1'){
			// 修改费率
			await updateMerchantRate(params)
			formParams.value.isOpenEdit = false
			uni.$utils.showToast('保存成功',1000,()=>{
				if(jsonData.merchantId){
					uni.navigateBack()
				}
				else{
					getMerchantRateData()
				}
				
			})
		}
		else{
			//设置费率
			await setMerchantRate(params)
			formParams.value.isOpenEdit = false
			uni.$utils.showToast('保存成功',1000,()=>{
				if(jsonData.merchantId){
					uni.navigateBack()
				}
				else{
					getMerchantRateData()
				}
			})
		}
	}
	// 获取商户费率
	const getMerchantRateData = async () => {
		let params = {}
		if (jsonData.merchantId) {
			params.ksnNo = jsonData.deviceNo
			params.merId = jsonData.merchantId
		} else {
			params.ksnNo = jsonData.ksnNo
		}
		const res = await getMerchantRate(params)
		const data = res.result
		rateInfo.value = data
		formParams.value.rateStatus = data.rateStatus || '0'
		//是否具有修改费率的权限 0：无 1：有
		const operateStatus = data.operateStatus || '0'
		//渠道类型chnlType：KDB：开店宝 HKD：必达，只有必达的才可以修改云闪付费率
		const chnlType = data.chnlType
		const list = data.rateInfoList
		formParams.value.rateList = []
		list.forEach(obj => {
			obj.chnlType = chnlType
			if (obj.payTypeCode == 'creditpay') {
				obj.value = obj.mercCreditCardRate
			} else {
				if (obj.chnlType == 'HKD') {
					if (obj.payTypeCode == 'nativepay') {
						///未配置取借记卡最大费率为扫码支付的费率，扫码支付不可修改费率
						if (formParams.value.rateStatus == '0') {
							obj.value = obj.debitCardRateMax
						} else {
							obj.value = obj.mercDebitCardRate
						}
					} else {
						obj.value = obj.mercDebitCardRate
					}
				} else {
					if (obj.payTypeCode == 'nativepay' || obj.payTypeCode == 'unionpay') {
						///未配置取借记卡最大费率为扫码支付/云闪付的费率，扫码支付/云闪付不可修改费率
						if (formParams.value.rateStatus == '0') {
							obj.value = obj.debitCardRateMax
						} else {
							obj.value = obj.mercDebitCardRate
						}
					} else {
						obj.value = obj.mercDebitCardRate
					}
				}
			}
			obj.inputValue = obj.value
			obj.isCanEdit = true
			formParams.value.rateList.push(obj)
			if (obj.payTypeCode == 'debitpay') {
				let model = {}
				model.retaName = '借记卡封顶'
				model.payTypeCode = '0'
				model.mercDebitCardTop = obj.mercDebitCardTop
				model.debitCardTopMin = obj.debitCardTopMin
				model.debitCardTopMax = obj.debitCardTopMax
				model.value = obj.mercDebitCardTop
				model.inputValue = model.value
				formParams.value.rateList.push(model)
			}
			if (obj.payTypeCode == 'creditpay') {
				let model = {}
				model.retaName = '秒到提现费'
				model.payTypeCode = '1'
				model.singleT0CostMin = obj.singleT0CostMin
				model.singleT0CostMax = obj.singleT0CostMax
				model.mercSingleT0Cost = obj.mercSingleT0Cost
				model.value = obj.mercSingleT0Cost
				model.inputValue = model.value
				singleRateModel.value = model
			}
		})
		formParams.value.isCanEdit = false
		if (jsonData.merchantId && operateStatus == '1') {
			formParams.value.isCanEdit = true
			formParams.value.isOpenEdit = true
		} else {
			if (operateStatus == '1' && jsonData.bindStatus != '1') {
				formParams.value.isCanEdit = true
				if (formParams.value.rateStatus == '0') {
					formParams.value.isOpenEdit = true
				} else {
					formParams.value.isOpenEdit = false
				}
			}
		}
	}
	onLoad(() => {
		getMerchantRateData()
	})
</script>

<style lang="scss" scoped>
	.devcie-rate-layout {
		.devcie-rate-header {
			background-color: #fff;

			.devcie-rate-header-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx;
				position: relative;

				.devcie-rate-header-item-right {
					color: $xh-red-color;
				}
			}
		}

		.devcie-rate-container {
			padding: 30rpx;

			.policy-config-form {
				background-color: #fff;
				border-radius: 20rpx;
			}

			.policy-config-form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 30rpx;
				position: relative;
				font-size: 26rpx;
				height: 100rpx;

				.policy-config-form-item-line {
					left: 30rpx;
					right: 30rpx;
				}

				text:nth-of-type(1) {
					flex-shrink: 0;
				}

				.range-text {
					color: $xh-red-color;
					font-size: 26rpx;
					flex-shrink: 0;
				}

				.value-text {
					color: #8E8D92;
					font-size: 26rpx;
					flex-shrink: 0;
				}
			}

			.policy-config-form-item-md {
				margin-top: 30rpx;
				border-radius: 20rpx;
				background-color: #fff;
			}

		}

		.devcie-rate-operate {
			padding-top: 30rpx;

			.submit-btn1 {
				margin-bottom: 30rpx;
			}

			.devcie-rate-operate-btn-edit {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 30rpx;
				color: $xh-theme-color;
			}
		}
	}
</style>